<?php
require('config.php');
	$user_id=$_REQUEST['user_id'];
	$msg_type=$_REQUEST['msg_type'];
	if($msg_type==0)
	{
		$sql="select h.*,m.id as xiaoxi_id,m.msg_type,h.userId as wb_user_id,m.userId as xiaoxi_user_id from ".$oto."_help_message as m,".$oto."_help as h  where m.userId='{$user_id}' and m.isDel='0' and h.id=m.helpId and (m.msg_type='0' or m.msg_type='2' ) order by m.createTime DESC";
		//file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$message[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(isset($message))
		{
			for($i=0;$i<count($message);$i++)
			{
				if($message[$i]['msg_type']==0)
				{
					$wb_id=$message[$i]['id'];
					$sql="select * from `".$oto."_help_order_user` where `help_id`='{$wb_id}' and isDel='0'";
					$result=$db->query($sql);
					while($row=$result->fetch_assoc()){
						$order_user[$i][]=$row;//将取得的所有数据赋值给person_info数组
					}
					if(isset($order_user[$i]))
					{
						$message[$i]['jiedan_num']=count($order_user[$i]);
					}
					else $message[$i]['jiedan_num']=0;
				}
				else if($message[$i]['msg_type']==2)
				{
					$wb_user_id=$message[$i]['wb_user_id'];
					$wb_id=$message[$i]['id'];
					$xiaoxi_user_id=$message[$i]['xiaoxi_user_id'];
					$sql="select ".$oto."_users.userPhoto,".$oto."_users.userName from `".$oto."_users` where `userId`='{$wb_user_id}'";
					$result=$db->query($sql);
					while($row=$result->fetch_assoc()){
						$user_info[$i][]=$row;//将取得的所有数据赋值给person_info数组
					}
					if(isset($user_info[$i]))
					{
						$message[$i]['for_xiaoxi_userPhoto']=$user_info[$i][0]['userPhoto'];
						$message[$i]['for_xiaoxi_userName']=$user_info[$i][0]['userName'];
					}
					$sql="select ".$oto."_help_chat.content from `".$oto."_help_chat` where `helpId`='{$wb_id}' and ((userId='{$wb_user_id}' and acceptUserId='{$xiaoxi_user_id}') or userId='{$xiaoxi_user_id}' and acceptUserId='{$wb_user_id}') and isDel='0' order by createTime DESC";
					//file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
					$result=$db->query($sql);
					while($row=$result->fetch_assoc()){
						$help_chat[$i][]=$row;//将取得的所有数据赋值给person_info数组
					}
					if(isset($help_chat[$i]))
					{
						$message[$i]['xiaoxi_content']=$help_chat[$i][0]['content'];
					}
				}
			}
			echo json_encode($message);
		}
		else
		{
			echo "[]";
		}
	}
	else if($msg_type==1)
	{
		$sql="select m.id as xiaoxi_id,m.msg_type,m.userId as xiaoxi_user_id,m.cat_id as wb_user_id,createTime from ".$oto."_help_message as m  where m.userId='{$user_id}' and m.helpId='0' and m.isDel='0' and m.msg_type='2' order by m.createTime DESC";
		//file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$message[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(isset($message))
		{
			for($i=0;$i<count($message);$i++)
			{
				if($message[$i]['msg_type']==2)
				{
					$wb_user_id=$message[$i]['wb_user_id'];
					$xiaoxi_user_id=$message[$i]['xiaoxi_user_id'];
					$sql="select * from ".$oto."_help_chat as m  where id='{$wb_user_id}'";
					$result=$db->query($sql);
					while($row=$result->fetch_assoc()){
						$wb_user_id_lin[$i][]=$row;//将取得的所有数据赋值给person_info数组
					}
					if(isset($wb_user_id_lin[$i]))
					{
						if($wb_user_id_lin[$i][0]['userId']==$xiaoxi_user_id)
						{
							$wb_user_id=$wb_user_id_lin[$i][0]['acceptUserId'];
							$message[$i]['wb_user_id']=$wb_user_id_lin[$i][0]['acceptUserId'];
						} 
						else
						{
							$wb_user_id=$wb_user_id_lin[$i][0]['userId'];
							$message[$i]['wb_user_id']=$wb_user_id_lin[$i][0]['userId'];
						} 
					}
					$sql="select ".$oto."_users.userPhoto,".$oto."_users.userName from `".$oto."_users` where `userId`='{$wb_user_id}'";
					$result=$db->query($sql);
					while($row=$result->fetch_assoc()){
						$user_info[$i][]=$row;//将取得的所有数据赋值给person_info数组
					}
					if(isset($user_info[$i]))
					{
						$message[$i]['for_xiaoxi_userPhoto']=$user_info[$i][0]['userPhoto'];
						$message[$i]['for_xiaoxi_userName']=$user_info[$i][0]['userName'];
					}
					$sql="select ".$oto."_help_chat.content from `".$oto."_help_chat` where `helpId`='0' and ((userId='{$wb_user_id}' and acceptUserId='{$xiaoxi_user_id}') or userId='{$xiaoxi_user_id}' and acceptUserId='{$wb_user_id}') and isDel='0' order by createTime DESC";
					//file_put_contents("tsxx.txt", "\r\n".$sql."\r\n", FILE_APPEND);
					$result=$db->query($sql);
					while($row=$result->fetch_assoc()){
						$help_chat[$i][]=$row;//将取得的所有数据赋值给person_info数组
					}
					if(isset($help_chat[$i]))
					{
						$message[$i]['xiaoxi_content']=$help_chat[$i][0]['content'];
					}
				}
			}
			echo json_encode($message);
		}
		else
		{
			echo "[]";
		}
	}
	
?>